package cn.ycc1.boot3mq.order.mq.service.impl;

import cn.ycc1.boot3mq.order.mq.service.OrderRabbitService;
import lombok.RequiredArgsConstructor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
/**
 * @author 张治保
 * date 2023/6/29
 */

@Service
@RequiredArgsConstructor
public class OrderRabbitServiceImpl implements OrderRabbitService {

    private final RabbitTemplate rabbitTemplate;
//    private final IShopOrderService shopOrderService;
//    private final IShopOrderItemService shopOrderItemService;
//
//
//    @Override
//    public void sendOrderClose(OrderInfo orderInfo) {
//        rabbitTemplate.convertAndSend(OrderRabbit.ORDER_CLOSE.exchange(), OrderRabbit.ORDER_CLOSE.routingKey(), orderInfo);
//    }
//
//    @Override
//    public void sendOrderDeliver(OrderPackageKeyDTO orderPackageKey) {
//        rabbitTemplate.convertAndSend(OrderRabbit.ORDER_DELIVER_GOODS.exchange(), OrderRabbit.ORDER_DELIVER_GOODS.routingKey(), orderPackageKey);
//    }
//
//    @Override
//    public void sendDelayConfirmReceive(OrderPackageKeyDTO orderPackageKey, Long delayTime, Long orderShopId) {
//        String orderNo = orderPackageKey.getOrderNo();
//        //检查是否包含其他未发货商品 如果包含则不发送自动延迟确认收货mq
//        Boolean existOtherWaitingDeliverItems = TenantShop.disable(
//                () -> shopOrderItemService.lambdaQuery()
//                        .eq(ShopOrderItem::getOrderNo, orderNo)
//                        .eq(ShopOrderItem::getShopId, orderShopId)
//                        .eq(ShopOrderItem::getStatus, ItemStatus.OK)
//                        .eq(ShopOrderItem::getPackageStatus, PackageStatus.WAITING_FOR_DELIVER)
//                        .exists()
//        );
//        //不包含则发送自动延迟确认收货mq
//        if (BooleanUtil.isTrue(existOtherWaitingDeliverItems)) {
//            return;
//        }
//        //更新店铺订单的发货时间
//        TenantShop.disable(() -> shopOrderService.lambdaUpdate()
//                .eq(ShopOrder::getOrderNo, orderNo)
//                .eq(ShopOrder::getShopId, orderShopId)
//                .setSql(SqlHelper.renderJsonSetSql("extra", Tuple.of("deliverTime", LocalDateTime.now().format(DatePattern.NORM_DATETIME_FORMATTER))))
//                .update());
//        rabbitTemplate.convertAndSend(
//                OrderRabbit.ORDER_AUTO_CONFIRM_RECEIPT.exchange(),
//                OrderRabbit.ORDER_AUTO_CONFIRM_RECEIPT.routingKey(),
//                orderPackageKey,
//                message -> {
//                    message.getMessageProperties().setHeader(MessageProperties.X_DELAY, delayTime);
//                    return message;
//                }
//        );
//    }
//
//    @Override
//    public void sendDelayEvaluate(OrderPackageKeyDTO orderPackageKey, Long delayTime, Long orderShopId) {
//        orderPackageKey.setShopId(orderShopId);
//        //更新店铺订单的确认收货时间
//        TenantShop.disable(() -> shopOrderService.lambdaUpdate()
//                .eq(ShopOrder::getOrderNo, orderPackageKey.getOrderNo())
//                .eq(ShopOrder::getShopId, orderPackageKey.getShopId())
//                .setSql(SqlHelper.renderJsonSetSql("extra", Tuple.of("receiveTime", LocalDateTime.now().format(DatePattern.NORM_DATETIME_FORMATTER))))
//                .update());
//        rabbitTemplate.convertAndSend(
//                OrderRabbit.ORDER_AUTO_COMMENT.exchange(),
//                OrderRabbit.ORDER_AUTO_COMMENT.routingKey(),
//                orderPackageKey,
//                message -> {
//                    message.getMessageProperties().setHeader(MessageProperties.X_DELAY, delayTime);
//                    return message;
//                }
//
//        );
//    }
//
//    @Override
//    public void sendOrderCreateFailed(OrderInfo orderInfo) {
//        rabbitTemplate.convertAndSend(OrderRabbit.ORDER_CREATE_FAILED.exchange(), OrderRabbit.ORDER_CREATE_FAILED.routingKey(), orderInfo);
//    }
//
//    @Override
//    public void sendOrderCreated(OrderCreatedDTO orderCreated) {
//        rabbitTemplate.convertAndSend(OrderRabbit.ORDER_CREATED.exchange(), OrderRabbit.ORDER_CREATED.routingKey(), orderCreated);
//    }
//
//    @Override
//    public void sendDelayOrderAutoClose(String oderNo, Long delayTime) {
//        rabbitTemplate.convertAndSend(
//                OrderRabbit.ORDER_AUTO_PAID_TIMEOUT_CLOSE.exchange(),
//                OrderRabbit.ORDER_AUTO_PAID_TIMEOUT_CLOSE.routingKey(),
//                oderNo,
//                message -> {
//                    message.getMessageProperties().setHeader(MessageProperties.X_DELAY, delayTime);
//                    return message;
//                }
//        );
//    }
//
//    @Override
//    public void sendOrderPaid(OrderPaidBroadcastDTO orderPaidBroadcast) {
//        rabbitTemplate.convertAndSend(OrderRabbit.ORDER_PAID_BROADCAST.exchange(), OrderRabbit.ORDER_PAID_BROADCAST.routingKey(), orderPaidBroadcast);
//    }
//
//    @Override
//    public void sendOrderFlush(OrderDetailInfoBO orderDetailInfo) {
//        rabbitTemplate.convertAndSend(
//                OrderRabbit.ORDER_CREATE.exchange(),
//                OrderRabbit.ORDER_CREATE.routingKey(),
//                orderDetailInfo
//        );
//    }
//
//    @Override
//    public void sendOrderEvaluate(OrderCompletedDTO orderCompleted) {
//        rabbitTemplate.convertAndSend(
//                OrderRabbit.ORDER_ACCOMPLISH.exchange(),
//                OrderRabbit.ORDER_ACCOMPLISH.routingKey(),
//                orderCompleted
//        );
//    }
//
//    @Override
//    public void sendAppletSubscribeMsg(SubscribeAppletMsgDTO subscribeApplet) {
//        rabbitTemplate.convertAndSend(
//                PigeonRabbit.PIGEON_APPLET_SUBSCRIBE.exchange(),
//                PigeonRabbit.PIGEON_APPLET_SUBSCRIBE.routingKey(),
//                subscribeApplet
//        );
//    }
//
//    /**
//     * 发送小程序订单发货消息
//     *
//     * @param orderPackageKey 订单
//     */
//    @Override
//    public void sendMiniAppOrderDeliver(OrderPackageKeyDTO orderPackageKey) {
//        //非快递配送，发货后发起微信物流录入
//        if (!LogisticsType.EXPRESS.equals(orderPackageKey.getLogisticsType())) {
//            orderPackageKey.setOrderUploadType(OrderUploadType.UPLOAD_SHIPPING_INFO);
//            orderPackageKey.setDeliveryMode(DeliveryMode.UNIFIED_DELIVERY);
//        } else {
//            //快递配送，查询除当前明细外，是否还有未发货正在售后的明细
//            List<ShopOrderItem> shopOrderItemList = TenantShop.disable(
//                    () -> shopOrderItemService.lambdaQuery()
//                            .eq(ShopOrderItem::getOrderNo, orderPackageKey.getOrderNo())
//                            .eq(ShopOrderItem::getPackageStatus, PackageStatus.WAITING_FOR_DELIVER)
//                            .eq(ShopOrderItem::getAfsStatus,
//                                    CollUtil.newArrayList(
//                                            AfsStatus.NONE, AfsStatus.REFUND_REQUEST, AfsStatus.REFUND_REJECT
//                                    )
//                            )
//                            .ne(ShopOrderItem::getProductId, orderPackageKey.getPackageId())
//                            .list()
//            );
//
//            orderPackageKey.setOrderUploadType(OrderUploadType.UPLOAD_COMBINED_SHIPPING_INFO);
//            orderPackageKey.setDeliveryMode(DeliveryMode.SPLIT_DELIVERY);
//            //判断是否最后一条未发货明细,不是直接结束
//            if (!CollUtil.isEmpty(shopOrderItemList)) {
//                return;
//            }
//        }
//        rabbitTemplate.convertAndSend(
//                OrderRabbit.MINI_APP_ORDER_DELIVER_GOODS.exchange(),
//                OrderRabbit.MINI_APP_ORDER_DELIVER_GOODS.routingKey(), orderPackageKey
//        );
//    }
}
